/*
 * Copyright 2017 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.config;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 类ViewResolverConfig.java的实现描述：ViewResolver设置
 * 
 * @author wujian 2017年4月7日 上午9:50:44
 */
@Configuration
public class RedisCacheConfiguration extends CachingConfigurerSupport {
    private Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private int    port;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.pool.max-idle}")
    private int    maxIdle;

    @Value("${redis.pool.max-wait}")
    private long   maxWaitMillis;

    @Bean
    public JedisPool redisPoolFactory() {

        logger.info("redis地址：" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = null;
        if (StringUtils.isEmpty(password)) {
            jedisPool = new JedisPool(jedisPoolConfig, host, port);
        } else {
            jedisPool = new JedisPool(jedisPoolConfig, host, port, 2000, password);
        }
        return jedisPool;
    }
}
